package cn.com.freelibra.forum.util.email;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestMapping;
public class EmailUtil {
	
	
	/**
	 * 
	 * @author Hdm
	 * @date 2017年3月26日
	 * @method 发送邮件验证码 
	 * @version 1.0
	 */
	public String SendEmail(String userName, String email, String host, String url, String sender, String username,
			String password) {
		String emailCaptcha = randomCount(6);
		if(email!=null&&!"".equals(email.trim())){
			String context="<a href='"+url+"/user/activationUser?userName="+userName+"&emailCaptcha="+emailCaptcha+"'>点击这里激活账号</a>";
			EmailTool.sendMian(email, "邮件验证码", context,host,sender,username,password);
		}
		return emailCaptcha;
	}
	/**
	 * 随机产生count位字符串
	 * @param count
	 * @return
	 */
	private String randomCount(int count) {
        String a = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        char[] rands = new char[count]; 
        for (int i = 0; i < rands.length; i++) 
        { 
            int rand = (int) (Math.random() * a.length()); 
            rands[i] = a.charAt(rand); 
        } 
        String emailCaptcha = "";
        for(int i=0;i<rands.length;i++){
        	emailCaptcha = emailCaptcha +rands[i];
        }
        return emailCaptcha.trim();
    }
	
}
